#!/usr/bin/env python3

from flask import Flask,make_response,url_for
from flask import render_template,send_file,request
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime


app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
#SQLALCHEMY_TRACK_MODIFICATIONS
app.config['SQLALCHEMY_DATABASE_URI'] = \
"mysql+pymysql://kong:111111@192.168.0.117/test"
db = SQLAlchemy(app)

class Users(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer,primary_key=True)
    email = db.Column(db.String,unique=True)
    username = db.Column(db.String,unique=True)
    passwd = db.Column(db.String)
    date = db.Column(db.DateTime)
## 增加数据

user = Users(id=5,email='sanle@sanle.com',username='sanle',passwd='sanlelearning',date=datetime.now())
db.session.add(user)
db.session.commit()


## 修改数据
#users = Users.query.filter_by(id=5).first()
#users.username='liaobingbing'
#db.session.add(users)
#db.session.commit()

## 删除数据
#users = Users.query.filter_by(id=5).first()
#db.session.delete(users)
#db.session.commit()

## 查询数据

#users = Users.query.all()
#print(users)
#db.session.commit()

#app = Flask(__name__)
#app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
#app.config['SQLALCHEMY_DATABASE_URI'] = \
# "mysql://root:111111@localhost/test"
#db = SQLAlchemy(app)



#@app.route('/')
#def v_index():
#    rsp = make_response('go <a href="%s">page2</a>' % url_for('v_page2'))
#    rsp.set_cookie('user','JJJDDDD')
#    return rsp

@app.route('/page2')
def v_page2():
    user = request.cookies['user']
    return 'you are %s' % user

#    return send_file('login.html')

#def hello_world():
#    return "hello world test one"

@app.route('/test',methods=["POST"])
def test():
    return 'post User'



#@app.route('/userid/<user_id>')
def show_userid(user_id):
    return 'User ID:{}'.format(user_id)
app.add_url_rule('/userid/<int:user_id>',view_func=show_userid)

@app.route("/login")
def hi():
    return render_template('login.html')

if __name__ == "__main__":
    #app.run("192.168.0.117",port=543)
    app.run(host='192.168.0.117',port=80,debug=True)
